<template>
  <div>
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @confirm="onUpdateBirthday"
      @cancel="$emit('close')"
    />
  </div>
</template>

<script>
import { updateUserFile } from '@/api/user'
import dayjs from 'dayjs'
export default {
  data () {
    return {
      minDate: new Date(1920, 0, 1),
      maxDate: new Date(), // 当前日期为最大可选日期
      currentDate: new Date(this.value)
    }
  },
  props: {
    value: {
      type: String
    }
  },
  methods: {
    async onUpdateBirthday () {
      const birthday = dayjs(this.currentDate).format('YYYY-MM-DD')
      this.$toast.loading({
        message: '更新中...',
        forbidClick: true,
        duration: 0
      })
      try {
        await updateUserFile({
          birthday
        })
        this.$toast('更新成功')
        this.$emit('input', birthday)
      } catch (err) {
        this.$toast('更新失败，请重试')
      }
    }
  }
}
</script>

<style lang="less" scoped>

</style>
